/*
 */
package com.ingenic.glass.camera;
import com.ingenic.glass.camera.CameraAppImpl;

import android.content.BroadcastReceiver;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.util.Log;
import android.content.Context;
import android.content.Intent;
import android.content.ComponentName;
import android.media.AudioManager;
import android.os.Build;
import android.os.storage.StorageManager;

import com.ingenic.glass.camera.QuickCapture;
import com.ingenic.glass.camera.live.CameraLive;
import com.ingenic.glass.camera.live.CameraIOTCLive;
import com.ingenic.glass.camera.live.CameraRTMPLive;

public class CameraButtonIntentReceiver extends BroadcastReceiver {

    private static final String TAG = "CameraButtonIntentReceiver";

    private final String CAMERA_LIVE_KEY = "com.ingenic.glass.camera.CameraLive";
    public static final String STATUS_LIVE_NOT_FINISH = "camera_live_not_finish";
    public static final String STATUS_LIVE_ERR_STOP = "camera_live_err_stop";

    private final String CAMERA_IOTC_LIVE_KEY = "com.ingenic.glass.camera.CameraIOTCLive";
    private final String CAMERA_RESOLUTION_SET_KEY = "camera_resoltuion_set";
    private final String LIVE_STOP_BY_NETWORK_BAD_KEY = "live_stop_by_network_bad_key";
    private final String CAMERA_RTMP_LIVE_KEY = "com.ingenic.glass.camera.CameraRTMPLive";

    // (vision: 短按录像键最多累计3次, 累计多次按键的识别条件:按键间隔不超过1秒钟)
    private static final int SHORT_PRESS_VIDEO_TIME_INTERVAL_MS = 1000000;//1s
    private static long mVideoCaptureTimeMillis = 0;

    @Override
	public void onReceive(Context context, Intent intent) {
	if (CameraAppImpl.DEBUG) Log.d(TAG, "onReceive action="+intent.getAction());
	StorageManager stoageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
	boolean isUsbMassStorageEnabled = stoageManager.isUsbMassStorageEnabled();
	Log.i(TAG, " isUsbMassStorageEnabled = "+ isUsbMassStorageEnabled + "--Build.BOARD = " + Build.BOARD);
	if ("cruise_ums".equalsIgnoreCase(Build.BOARD) && isUsbMassStorageEnabled) {
		return;
	}
	if (MediaStore.ACTION_IMAGE_CAPTURE.equals(intent.getAction())) {
	    // short press key
	    if (CameraAppImpl.DEBUG) Log.d(TAG, "Action is ACTION_IMAGE_CAPTURE.");
	    if ((null == VideoActivity.getMInstance())
		&& (null == PhotoActivity.getMInstance())
		&& (null == CameraLive.getMInstance())
		&& (null == QuickCapture.getMInstance())
		&& (null == CameraIOTCLive.getMInstance())
		&& (null == CameraRTMPLive.getMInstance())) {
		QuickCapture quickCapture = new QuickCapture(context);
		quickCapture.start();
	    } else if (null != PhotoActivity.getMInstance()) {
		PhotoActivity.getMInstance().takePicture();
	    } else if (null != VideoActivity.getMInstance()) {
		VideoActivity.getMInstance().takePicture();
	    } else if (null != CameraLive.getMInstance()) {
		CameraLive.getMInstance().takePicture();
	    } else if (null != CameraIOTCLive.getMInstance()) {
		CameraIOTCLive.getMInstance().takePicture();
	    } else if (null != CameraRTMPLive.getMInstance()) {
		CameraRTMPLive.getMInstance().takePicture();
	    }
	    
	} else if (MediaStore.ACTION_VIDEO_CAPTURE.equals(intent.getAction())) {
	    if (CameraAppImpl.DEBUG) Log.d(TAG, "Action is ACTION_VIDEO_CAPTURE.");
	    if (CameraAppImpl.IS_CRUISE_BOARD) {
		if(CameraLive.getMInstance().getMInstance() != null){
		    CameraLive.getMInstance().getMInstance().finish(); 	 //stop video live
		}else if (CameraIOTCLive.getMInstance().getMInstance() != null) {
		    CameraIOTCLive.getMInstance().getMInstance().finish();
		} else if (CameraRTMPLive.getMInstance().getMInstance() != null) {
		    CameraRTMPLive.getMInstance().getMInstance().finish();
		}else if (VideoActivity.getMInstance() != null) {
		    if (CameraAppImpl.DEBUG) Log.d(TAG, "--stop video");
		    VideoActivity.getMInstance().finish();
		} else if (QuickCapture.getMInstance() == null){
		    Intent intentVideo = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
		    intentVideo.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		    if (CameraAppImpl.DEBUG) Log.d(TAG, "--start video");
		    context.startActivity(intentVideo);
		}else{
		    Log.e(TAG,"camera is working.so recode be refused!");
		}
	    }else if("vision".equalsIgnoreCase(Build.BOARD)) {
		if (VisionVideoActivity.getMInstance() == null) {
			Intent startIntent = new Intent(context, VisionVideoActivity.class);
			startIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			context.startActivity(startIntent);
		}else {
			long nowMillis = System.currentTimeMillis();
			long timeInterval = nowMillis - mVideoCaptureTimeMillis;
			if (CameraAppImpl.DEBUG) Log.d(TAG, "timeInterval = "+timeInterval);
			if(timeInterval <= SHORT_PRESS_VIDEO_TIME_INTERVAL_MS) {
				VisionVideoActivity.getMInstance().addSegmentCount();
			}
		}
		mVideoCaptureTimeMillis = System.currentTimeMillis();
	    }else{
		if (CameraAppImpl.DEBUG) Log.d(TAG, "Action is ACTION_VIDEO_CAPTURE.");
		AudioManager audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
		if (audioManager.getMode() == AudioManager.MODE_IN_CALL && 
		    !CameraAppImpl.NO_SCREEN_DEVICE) {
		    Log.w(TAG, "not support VideoRecording when is MODE_IN_CALL for device with screen.");
		    return;
		}
			
		if (null != VideoActivity.getMInstance()) {
		    VideoActivity.getMInstance().finish();
		} else if ((null == PhotoActivity.getMInstance())
			   && (null == CameraLive.getMInstance())
			   && (null == CameraIOTCLive.getMInstance())
			   && (null == CameraRTMPLive.getMInstance())
			   && (null == QuickCapture.getMInstance())) {
		    Intent intentVideo = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
		    intentVideo.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		    context.startActivity(intentVideo);
		}
	    }
		    
	} else if (this.CAMERA_LIVE_KEY.equals(intent.getAction())) {
	    if (CameraAppImpl.DEBUG) Log.d(TAG, "Action is " + this.CAMERA_LIVE_KEY + " finish"+ CameraLive.getMInstance());
	    if (null != VideoActivity.getMInstance()) {
		VideoActivity videoActivity = VideoActivity.getMInstance();
		videoActivity.setIsCameraLiveIn(true);
		videoActivity.finish();
	    } else if (null != PhotoActivity.getMInstance()) {
		PhotoActivity.getMInstance().finish();
	    } else if (null != CameraLive.getMInstance()) {
		CameraLive.getMInstance().setErrorState(STATUS_LIVE_NOT_FINISH);	
		CameraLive.getMInstance().finish();
		return;
	    } else if (null != QuickCapture.getMInstance()) {
		QuickCapture.getMInstance().finish();
	    } else if (null != CameraIOTCLive.getMInstance()) {
		    CameraIOTCLive.getMInstance().finish();
	    }else if (null != CameraRTMPLive.getMInstance()) {
		    CameraRTMPLive.getMInstance().finish();
	    }
	    Intent cameraLiveIntent = new Intent(context, CameraLive.class);
	    cameraLiveIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
	    context.startActivity(cameraLiveIntent);
	} else if (this.CAMERA_IOTC_LIVE_KEY.equals(intent.getAction())) {
	    if (CameraAppImpl.DEBUG)
		Log.d(TAG, "Action is " + this.CAMERA_IOTC_LIVE_KEY
		      + " finish" + CameraIOTCLive.getMInstance());
	    if (null != VideoActivity.getMInstance()) {
		VideoActivity videoActivity = VideoActivity.getMInstance();
		videoActivity.setIsCameraLiveIn(true);
		videoActivity.finish();
	    } else if (null != PhotoActivity.getMInstance()) {
		PhotoActivity.getMInstance().finish();
	    } else if (null != CameraIOTCLive.getMInstance()) {
		// CameraIOTCLive.getMInstance().setErrorState(
		// 		STATUS_LIVE_NOT_FINISH);
		boolean isStopLiveByNetworkBad = intent.getBooleanExtra(LIVE_STOP_BY_NETWORK_BAD_KEY, false);
		if (isStopLiveByNetworkBad) {
			CameraIOTCLive.getMInstance().setErrorState(
				STATUS_LIVE_ERR_STOP);
		}
		CameraIOTCLive.getMInstance().finish();
		return;
	    } else if (null != QuickCapture.getMInstance()) {
		QuickCapture.getMInstance().finish();
	    } else if (null != CameraLive.getMInstance()) {
		CameraLive.getMInstance().finish();
	    } else if (null != CameraRTMPLive.getMInstance()) {
		CameraRTMPLive.getMInstance().finish();
	    }
	    int cameraSet = intent.getIntExtra(CAMERA_RESOLUTION_SET_KEY, 0);
	    Intent cameraLiveIntent = new Intent(context,
						 CameraIOTCLive.class);
	    cameraLiveIntent.putExtra(CAMERA_RESOLUTION_SET_KEY, cameraSet);
	    cameraLiveIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
	    context.startActivity(cameraLiveIntent);
	} else if (this.CAMERA_RTMP_LIVE_KEY.equals(intent.getAction())) {
	    if (CameraAppImpl.DEBUG)
		Log.d(TAG, "Action is " + this.CAMERA_RTMP_LIVE_KEY
		      + " finish" + CameraRTMPLive.getMInstance());
	    if (null != VideoActivity.getMInstance()) {
		VideoActivity videoActivity = VideoActivity.getMInstance();
		videoActivity.setIsCameraLiveIn(true);
		videoActivity.finish();
	    } else if (null != PhotoActivity.getMInstance()) {
		PhotoActivity.getMInstance().finish();
	    } else if (null != CameraRTMPLive.getMInstance()) {
		CameraRTMPLive.getMInstance().finish();
		return;
	    } else if (null != QuickCapture.getMInstance()) {
		QuickCapture.getMInstance().finish();
	    } else if (null != CameraIOTCLive.getMInstance()) {
		CameraIOTCLive.getMInstance().finish();
	    } else if (null != CameraIOTCLive.getMInstance()) {
		CameraLive.getMInstance().finish();
	    }
	    Intent cameraLiveIntent = new Intent(context,
						 CameraRTMPLive.class);
	    String liveUrl = intent.getStringExtra("liveUrl");
	    if (TextUtils.isEmpty(liveUrl)) {
	            Log.e(TAG, "liveUrl is empty.");
	            return ;
	    }
	    cameraLiveIntent.putExtra("liveUrl", liveUrl);
	    cameraLiveIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
	    context.startActivity(cameraLiveIntent);
	}
    }
}
